package Niuke;

/**
 * @author: leiwenkang
 * @date: 2020/5/20
 * @time: 13:12
 */
public class ReorderTest {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
        reOrderArray(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }
    public static void reOrderArray(int [] array) {
        if (array == null || array.length == 0) {
            return;
        }

        int i = 0;
        for(; i < array.length; i++) {
            //找到奇数
            if ((array[i] & 1) == 1) {
                //检查之前的数字中，有没有偶数，若有偶数，则将偶数的都后移
                int val = array[i];
                int j = i - 1;
                boolean flag = false;
                for(; j >= 0 && ((array[j] & 1) == 0); j--) {
                    array[j+1] = array[j];
                    flag = true;
                }
                if (flag) {
                    array[j + 1] = val;
                }
            }
        }
    }
}
